package originutils;

import java.util.Comparator;
import java.util.HashSet;
import java.util.Optional;
import java.util.function.ToLongFunction;
import org.testng.Assert;
import org.testng.annotations.Test;

public class Comparator1 {

    /**
     * 多于一个元素时，comparingLong比较方法才会执行
     */
    @Test
    private void t1() {
        var set = new HashSet<Long>();
        set.add(1L);
        set.add(2L);
        set.add(3L);

        Optional<Long> max = set.stream().max(Comparator.comparingLong(this::getLong));
        Long aLong = max.get();
        Assert.assertEquals(Optional.of(aLong), 3L);

    }

    /**
     * 如果只有一个元素，comparingLong比较方法不会执行
     */
    @Test
    private void t2() {
        var set = new HashSet<Long>();
        set.add(1L);
        Optional<Long> max = set.stream().max(Comparator.comparingLong(this::getLong));


    }

    private Long getLong(Long n) {
        return n * 2L;
    }
}
